function formatClassifyName(value,row,index) {
    console.log(value);

    if(value){
        return value.name;
    }
}

function formatEmployeeName(value,row,index) {
    console.log(value);
    if(value){
        return value.name;
    }
}


$(function () {
    var payment_datagrid=  $("#payment_datagrid");
    var payment_dialog=  $("#payment_dialog");
    var payment_add_form= $("#payment_add_form");

    var rows = payment_datagrid.datagrid("getRows");


    var cmdObj={
        //导出
        exportPayment: function () {
            window.location.href = "/payment/export";
        },
        //高级查询
        searchPayment:function () {
            //查询input中的值
            var keytime=$("#keytime").val();
            //调用datagrid,带上关键字重新加载数据
            payment_datagrid.datagrid("load",{keytime:keytime});
        },
        reloadPayment:function() {
            payment_datagrid.datagrid("reload");
        },
        cancel:function(){
            payment_dialog.dialog("close")
        },
        addPayment:function() {

            payment_add_form.form("clear");
            payment_dialog.dialog("setTitle","添加");
            payment_dialog.dialog("open");
        },
        editPayment:function() {
            var selectedRow= payment_datagrid.datagrid("getSelected");
            console.log(selectedRow);
            if(!selectedRow){
                $.messager.alert("提示","请选择一行","error");
            }

            payment_add_form.form("clear");

            //$("#a").combobox("setValue",selectedRow.payclassify.id);
            //$("#b").combobox("setValue",selectedRow.employee.id);
            if(selectedRow.payclassify){
                selectedRow['payclassify.id']=selectedRow.payclassify.id;
            }
            if(selectedRow.employee){
                selectedRow['employee.id']=selectedRow.employee.id;
                selectedRow['employee.name']=selectedRow.employee.name;
            }
            payment_add_form.form("load",selectedRow);
            payment_dialog.dialog("setTitle","修改");
            payment_dialog.dialog("open");
        },
        save:function() {
            var url="/payment/save";
            var paymentId= $("#payment_id").val();
            if(paymentId){
                url="/payment/update"
            }
            payment_add_form.form("submit",{
                url:url,
                success:function(data) {
                    data=$.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            cmdObj.cancel();
                            cmdObj.reloadPayment();
                        })
                    } else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            })
        },
        deletePayment:function() {
            var selectedRow= payment_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("提示","请选择一行","error");
            }
            $.messager.confirm("提示","是否确定删除",function (yes) {
                if(yes){
                    //发送ajax请求
                    $.post("/payment/delete",{id:selectedRow.id},function (data) {
                        if(data.success){
                            $.messager.alert("提示","操作成功","info",function () {
                                cmdObj.cancel();
                                cmdObj.reloadPayment();
                            })
                        }else {
                            $.messager.alert("提示",data.msg,"error")
                        }
                    })
                }

            })
        }
    }

    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        //cmd = "addEmp";
        //cmdObj[cmd]  :根据cmd的值,找到 cmdObj 对应的方法
        //cmdObj[cmd]() : () ,触发方法的执行
        cmdObj[cmd]();
    });

})

